import { Skill } from "../class/skill"
import RoomType from "../entity/RoomType"
import { ConfigData } from "../interface/config_data"
import { Utils } from "../utils/utils"
import JsonManager from "./json_manager"
import StorageManager from "./storage_manager"

const { ccclass, property } = cc._decorator
/**
 * 此文件用于控制游戏中所有数据 以及可视化绑定
 */
@ccclass
export default class DD extends cc.Component {
    static _instance: DD = null

    static get instance() {
        if (this._instance == null) {
            this._instance = new DD()
        }
        return this._instance
    }
    config: ConfigData = null

    unlockMap = { 1: 4, 2: 8, 3: 12, 4: 16 }

    //道具仓库
    itemDepot: { [key: number]: number } = {}
    winItems: number[] = []
    peneCount: number = 0
    doubleBallRAte: number = 0
    roomType:RoomType
    rooms: RoomType[]
    caculateAtkEtraRate() {
        let res = 0
        let extra = JsonManager.instance.getDataByName('stat_extra')
        StorageManager.instance.levelInfo.statIds.forEach(v => {
            let a = extra[v + 1];
            if (a && a.type == "atk") {
                res += a.value
            }
        })
        // cc.log("额外atk倍率"+res)
        return res * 0.01
    }

    getStatName(name) {
        let map = { 'atk': '攻击力', 'range': '范围', 'hp': '血量', 'splitNum': '分裂次数' }
        return map[name] ? map[name] : ''
    }


    caculateHpEtraRate() {
        let res = 0
        let extra = JsonManager.instance.getDataByName('stat_extra')
        StorageManager.instance.levelInfo.statIds.forEach(v => {
            let a = extra[v + 1];
            if (a && a.type == "hp") {
                res += a.value
            }
        })
        // cc.log("额外hp"+res)
        return res
    }

    /**
   * 判断是否足够
   * @param cost 
   * @param tip 提示
   */
    checkEnough(cost: { [key: number]: number }, tip: boolean = false) {
        let have = true
        let noHaveId = []
        for (let id in cost) {
            if (this.itemDepot[id]) {
                if (cost[id] > this.itemDepot[id]) {
                    have = false
                    noHaveId.push(+id)
                }
            } else {
                have = false
                noHaveId.push(+id)
            }
        }
        if (tip && !have) {

        }
        return have
    }
    /**
     * 获得某个物品
     */
    onReward(costData) {
        for (let itemId in costData) {

        }
    }

    /**
     * 花费某个物品
     */
    onCost(costData) {
        for (let itemId in costData) {
            if (this.itemDepot[itemId]) {
                this.itemDepot[itemId] -= costData[itemId]
            }
        }
    }

    setAttackDAta() {
        let skills = JsonManager.instance.getDataByName('skill');
        let skillspecial = JsonManager.instance.getDataByName('skill_special');
        let range = 100;
        let bulletCount = 1;
        let peneCount = 0;//穿透
        let spreadCount = 1;
        let carCount = 1;
        let carCd = 12;
        let ballCount = 1
        let ballstregth = 0;
        let ballSpeed = 0

        StorageManager.instance.skills.forEach(id => {
            let effect: Skill = skills[id]

            switch (effect.type) {
                case 'carCount':
                    carCount++
                case 'ballStrenth':
                    ballstregth += (effect.damage * 0.01)
                case 'ballCount':
                    ballCount++
                case 'ballSpeed':
                    ballSpeed += (effect.damage * 0.01)
                case 'carCd':
                    carCd--
                case "spreadCount":
                    spreadCount += 1;
                    break;
                case "bulletCount":
                    bulletCount += effect.damage;
                    break;
                case "explosion":
                    StorageManager.instance.canExplode = effect.damage;
                    break;
                case 'peneCount':
                    this.peneCount += effect.damage
                    break;
                case 'peneCount':
                    this.peneCount += effect.damage
                    break;
                case "explosionRadiusMultiplier":
                    range += effect.damage
                    break;
            }
        });
        let minTime = 6
        this.doubleBallRAte = 0
        this.setUnlockSpiecial()
        StorageManager.instance.skillSpeicialIds.forEach(id => {
            let effect: Skill = skillspecial[id]

            switch (effect.type) {
                case 'doubleBall':
                    this.doubleBallRAte += effect.value

            }
        });

        cc.log("双重事发:" + this.doubleBallRAte)
        StorageManager.instance.carCd = carCd >= minTime ? carCd : minTime;
        StorageManager.instance.carCount = carCount;
        StorageManager.instance.ballCount = ballCount;
        StorageManager.instance.ballStrenth = ballstregth;
        StorageManager.instance.spreadCount = spreadCount;
        StorageManager.instance.explosionRadius = range * 0.01;
        StorageManager.instance.playerData.bulletCount = bulletCount;
    }

    setUnlockSpiecial() {
        StorageManager.instance.skillSpeicialIds = []
        let specidl = JsonManager.instance.getDataByName('skill_special')
        let tmp = Object.values(specidl)

        // PoolManager
        tmp.forEach((data, index) => {
            if (StorageManager.instance.skillLevel[data.skillId] >= this.unlockMap[index + 1]) {

                StorageManager.instance.skillSpeicialIds.push(data.id)

            }



            // this.content1.addChild(item); // 添加到 Content
        });
        cc.log("结算")
        cc.log(StorageManager.instance.skillSpeicialIds)
    }

    setWinItem() {

        let ids1 = [3, 5, 7, 11]
        let ids2 = [1, 2, 3, 4]
        // Utils
        let num = Utils.getRandomNumInRange(1, 3)
        ids1 = Utils.getArrRandomItemsByNum(ids1, num);
        ids1.forEach(id => {
            this.winItems.push(id)
            StorageManager.instance.skillTicketCost[id] += 50
        })
        ids2 = Utils.getArrRandomItemsByNum(ids2, num);
        ids2.forEach(id => {
            this.winItems.push(id)
            StorageManager.instance.charactorTicket[id] += 50
        })


    }
}